'use strict';

const uidObj = require('uni-id');
const {
	Controller
} = require('uni-cloud-router');
const dbcmd = uniCloud.database().command;

module.exports = class SeckillController extends Controller {

	// 秒杀中
	async data() {
		let {
			shop_id
		} = this.ctx.data;
		let response = { 
			code: 1,
		};
		if(!shop_id){
			response.code = 1;
			response.msg = '店铺ID为空';
			return response
		}
		const start = Date.now();
		const nowdate = start;
		
		// 获取秒杀中的数据
		const seckillRes = await this.db.collection('wmzmall-market-seckill')
			.where({
				shop_id:shop_id,
				is_delete: 0,
				state: '启用',
				seckill_begin_time: this.db.command.lt(nowdate),
				seckill_end_time: this.db.command.gt(nowdate)
			})
			.limit(1)
			.get();
		
		[response.datas] = seckillRes.data;
		// 活动到期时间
		if (response.datas && response.datas._id) response.datas.expires_time = (response.datas.seckill_end_time - Date.now()) / 1000;
		
		const end = Date.now();
		console.log(`耗时：${end - start}ms`);
		response.code = 0;
		response.msg = `耗时：${end - start}ms`;
		return response;
	}
	
}
